//
//  MinInterfaceViewController.m
//  电子书阅览器
//
//  Created by apple on 16/12/8.
//  Copyright © 2016年 LWB. All rights reserved.
//

#import "MinInterfaceViewController.h"
#import "ReadingViewController.h"
#import "ReaderViewController.h"
#import "LWBNavigationViewController.h"
@interface MinInterfaceViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSArray *dataArray;
@end

@implementation MinInterfaceViewController

-(NSArray *)dataArray{
    if (_dataArray ==nil) {
        NSMutableArray *arr = [NSMutableArray array];
        for (int i=1; i<18; i++) {
            [ arr addObject:[NSString stringWithFormat:@"i-%d",i]];
        }
        _dataArray =arr;
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _tbView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    [self.view addSubview:_tbView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count/3+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
-(UIView *)setUpCell:(NSInteger )tap{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 110)];
    NSInteger top =(tap+1)*3;
    NSInteger bum = tap==0?1:tap*3+1;
    CGFloat interval = 15;
    CGFloat margin  =10;
    for (int i=(int)bum;i<=(int)top;i++) {
        CGFloat bookW = (self.view.frame.size.width-2*interval-margin*2)/3;
        CGFloat bookX=i%3==0?(self.view.frame.size.width-margin-bookW):(i%3==1?margin:margin+bookW+interval);
        UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(bookX, 5, bookW, 90)];
        but.tag =i;
        NSLog(@"%d",i);
        [but setImage:[UIImage imageNamed:[NSString stringWithFormat:@"i-%ld",(NSInteger)i]] forState:UIControlStateNormal];
        [but addTarget:self action:@selector(bookReading:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:but];
    }
    return view;
}
-(void)bookReading:(UIButton *)but{
    ReadingViewController *read = [[ReadingViewController alloc]init];
    LWBNavigationViewController *nav = [[LWBNavigationViewController alloc]init];
//   ReaderViewController *read = [ReaderViewController new];
//    NSError *err;
//    read.text = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"/Users/apple/Desktop/IOS/电子书阅览器/电子书阅览器/i-%ld.txt",(long)but.tag] encoding:NSUTF8StringEncoding error:&err];
    //con.text = content;
//    read.hidesBottomBarWhenPushed = NO;
//    NSLog(@"%@",read.text);
    read.flag =[NSString stringWithFormat:@"i-%ld",but.tag];
    //创建动画
    CATransition *animation = [CATransition animation];
    //设置运动轨迹的速度
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    //设置动画类型为立方体动画
    animation.type = @"oglFlip";
    //设置动画时长
    animation.duration =0.9f;
    //设置运动的方向
    animation.subtype =kCATransitionFromRight;
    //控制器间跳转动画
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:animation forKey:nil];
    [nav pushViewController:read animated:YES];
    [self presentViewController:nav animated:NO completion:nil];
  //  [self presentViewController:nav animated:nil completion:nil];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    UIImage *backImage = [UIImage imageNamed:@"BookShelfCell"];
    backImage = [backImage stretchableImageWithLeftCapWidth:backImage.size.width*0.5 topCapHeight:backImage.size.height*0.54];
    cell.backgroundView = [[UIImageView alloc]initWithImage:backImage];
    [cell.contentView addSubview:[self setUpCell:indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
